package organization

import (
	"context"
	"git.myscrm.cn/golang/stark/v4"
	"gorm.io/gorm"
	"time"
)

type CommonOrganizationRepository struct {
}

func NewOrganizationRepository() CommonOrganizationRepoIface {
	return &CommonOrganizationRepository{}
}
//
//func (r *CommonOrganizationRepository) GetInfoByAccount(ctx context.Context, tenantCode, account string) (*CommonUserInfoBo, error) {
//	db := stark.MySQLConn.Get(ctx)
//	commonUserInfoBo := &CommonUserInfoBo{}
//	err := db.Table(CommonOrganization{}.TableName()).Where("tenant_code =? and user_code = ? and is_deleted = 0 ", tenantCode, account).First(&commonUserInfoBo).Error
//	if err != nil && err != gorm.ErrRecordNotFound {
//		return nil, err
//	}
//	return commonUserInfoBo, nil
//}
//
//func (r *CommonOrganizationRepository) GetInfoByAccountAndPassword(ctx context.Context, tenantCode, account, passwordMd5 string) (*CommonUserInfoBo, error) {
//	db := stark.MySQLConn.Get(ctx)
//	commonUserInfoBo := &CommonUserInfoBo{}
//	err := db.Table(CommonOrganization{}.TableName()).Where("tenant_code =? and user_code = ? and password=? and is_deleted = 0 ", tenantCode, account, passwordMd5).First(&commonUserInfoBo).Error
//	if err != nil && err != gorm.ErrRecordNotFound {
//		return nil, err
//	}
//	return commonUserInfoBo, nil
//}
//
//func (r *CommonOrganizationRepository) GetInfoByUid(ctx context.Context, uid string) (*CommonUserInfoBo, error) {
//	db := stark.MySQLConn.Get(ctx)
//	commonUserInfoBo := &CommonUserInfoBo{}
//	err := db.Table(CommonOrganization{}.TableName()).Where("uid = ? and is_deleted = 0", uid).First(&commonUserInfoBo).Error
//	if err != nil && err != gorm.ErrRecordNotFound {
//		return nil, err
//	}
//	return commonUserInfoBo, nil
//}

////获取绑定用户详情
//func (r *CommonOrganizationRepository) GetInfoWithBind(ctx context.Context, condition map[string]interface{}) (*CommonUserInfoBo, error) {
//	db := stark.MySQLConn.Get(ctx)
//
//	//commonUserBind := &mysql.CommonUserBind{}
//	//if mpOpenid, ok := condition["mp_openid"]; ok {
//	//	delete(condition, "mp_openid")
//	//	condition["user_uid"] =
//	//	err := db.Where("mp_openid =?", mpOpenid).Order("created_time desc").First(&commonUserBind).Error
//	//	if err != nil && err != gorm.ErrRecordNotFound {
//	//		return nil, err
//	//	}
//	//}
//	commonUserInfoBo := &CommonUserInfoBo{}
//	err := db.Select(CommonOrganization{}.TableName() + ".*,common_user_bind.mp_openid").Table(CommonOrganization{}.TableName()).Joins("inner JOIN common_user_bind ON " + CommonOrganization{}.TableName() + ".uid = common_user_bind.common_user_data_uid").Where(condition).Where("common_user_data.is_deleted = 0 and common_user_bind.is_deleted = 0").First(&commonUserInfoBo).Error
//	if err != nil && err != gorm.ErrRecordNotFound {
//		return nil, err
//	}
//	return commonUserInfoBo, nil
//}
//
//func (r *CommonOrganizationRepository) GetInfoLeftJoinBind(ctx context.Context, condition map[string]interface{}) (*CommonUserInfoBo, error) {
//	db := stark.MySQLConn.Get(ctx)
//	commonUserInfoBo := &CommonUserInfoBo{}
//	err := db.Select(CommonOrganization{}.TableName() + ".*,common_user_bind.mp_openid").Table(CommonOrganization{}.TableName()).Joins("left JOIN common_user_bind ON " + CommonOrganization{}.TableName() + ".uid = common_user_bind.common_user_data_uid").Where(condition).Where("common_user_data.is_deleted = 0").Order("created_time desc").First(&commonUserInfoBo).Error
//	if err != nil && err != gorm.ErrRecordNotFound {
//		return nil, err
//	}
//	return commonUserInfoBo, nil
//}

func (r *CommonOrganizationRepository) GetInfo(ctx context.Context, condition map[string]interface{}, orderBy string) (*CommonOrganization, error) {
	db := stark.MySQLConn.Get(ctx)
	commonUserData := &CommonOrganization{}
	err := db.Where(condition).Where("is_deleted = 0").Order(orderBy).First(&commonUserData).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return commonUserData, nil
}

//获取列表数据
func (r *CommonOrganizationRepository) GetList(ctx context.Context, condition map[string]interface{}, page, pagesize int, orderBy string) (int64, []*CommonOrganization, error) {
	db := stark.MySQLConn.Get(ctx)

	commonUserDataList := []*CommonOrganization{}
	var count int64 = 0
	if page < 1 {
		page = 1
	}
	if pagesize < 1 {
		pagesize = 10
	}
	offset := (page - 1) * pagesize

	query := db.Where(condition)
	err := query.Count(&count).Error
	if err != nil {
		return 0, nil, err
	}
	query = query.Offset(offset).Limit(pagesize).
		Find(&commonUserDataList)
	if orderBy != "" {
		query = query.Order(orderBy)
	}
	err = query.Error
	if err != nil {
		return 0, nil, err
	}
	return count, commonUserDataList, nil
}

//更新数据
func (r *CommonOrganizationRepository) Update(ctx context.Context, data, condition map[string]interface{}) (bool, error) {
	db := stark.MySQLConn.Get(ctx)

	err := db.Model(&CommonOrganization{}).Where(condition).Updates(data).Error
	if err != nil {
		return false, err
	}
	return true, nil
}

//插入数据
func (r *CommonOrganizationRepository) Insert(ctx context.Context, commonUserData *CommonOrganization) (bool, error) {
	db := stark.MySQLConn.Get(ctx)

	err := db.Save(commonUserData).Error
	if err != nil {
		return false, err
	}

	return true, nil
}

//逻辑删除数据
func (r *CommonOrganizationRepository) Delete(ctx context.Context, condition map[string]interface{}) (bool, error) {
	updateData := map[string]interface{}{}
	updateData["is_deleted"] = 1
	updateData["modified_time"] = time.Now()
	db := stark.MySQLConn.Get(ctx)

	err := db.Model(&CommonOrganization{}).Where(condition).Updates(updateData).Error
	if err != nil {
		return false, err
	}
	return true, nil
}

func (r *CommonOrganizationRepository) GetOrganizationInfo(ctx context.Context, condition map[string]interface{}) (*CommonOrganization, error) {
	db := stark.MySQLConn.Get(ctx)
	commonOrganization := &CommonOrganization{}
	err := db.Table(commonOrganization.TableName()).Where(condition).First(&commonOrganization).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	if err == gorm.ErrRecordNotFound {
		return commonOrganization, nil
	}
	return commonOrganization, nil
}
